<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StorePersonRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $validate_rules = [
            'name' => 'required|max:20|min:1',
            'id_number' => 'required|unique:persons|max:50',
            'reg_addr' => 'required|max:50',
            'temp_addr' => 'required|max:50',
            'five_saw_time' => 'required|date',
            'meet_situation' => 'max:255',
            'remark' => 'max:2000',
            'phone' => 'required|unique:persons|max:50',
            'qq' => 'max:50',
            'weixin' => 'max:50',
        ];

        // 离开时间只有不为空时才验证
        if (! empty($this->leave_time)) {
            $validate_rules['leave_time'] = 'date|after:five_saw_time';
        }
        return $validate_rules;
    }

    /**
     * Get custom messages for validator errors.
     *
     * @return array
     */
    public function messages()
    {
        return [
            'id_number.unique' => '身份证号重复',
            'phone.unique' => '手机号已存在',
            'meet_situation.max' => '见面情况不能超过255个字符',
            'remark.max' => '我市活动情况不能超过2000个字符',
            'leave_time.after_or_equal' => '离开时间不能早于到我市时间',
            'five_saw_time.date' => '来我市时间日期错误',
        ];
    }
}
